from pydantic import BaseModel, Field, validator
from typing import Optional
from uuid import UUID
from datetime import datetime
from app.models.maintenance_record import MaintenanceType
from app.models.maintenance_plan import PlanFrequency

# 维护计划基础模型
class MaintenancePlanBase(BaseModel):
    maintenance_type: MaintenanceType
    title: str = Field(..., min_length=1, max_length=255)
    description: Optional[str] = None
    priority: str = Field(default="medium", pattern="^(low|medium|high|urgent)$")
    frequency: PlanFrequency
    interval_days: Optional[int] = Field(None, gt=0, description="自定义间隔天数")
    estimated_duration: Optional[int] = Field(None, gt=0, description="预计持续时间(分钟)")
    start_date: datetime
    end_date: Optional[datetime] = None
    default_assignee: Optional[UUID] = None
    advance_notice_days: int = Field(default=3, ge=0, le=30)
    template_description: Optional[str] = None
    template_checklist: Optional[str] = None

    @validator('interval_days')
    def validate_interval_days(cls, v, values):
        if values.get('frequency') == PlanFrequency.CUSTOM and v is None:
            raise ValueError('当频率为自定义时，必须指定间隔天数')
        return v

    @validator('end_date')
    def validate_end_date(cls, v, values):
        if v and values.get('start_date') and v <= values['start_date']:
            raise ValueError('结束日期必须晚于开始日期')
        return v

class MaintenancePlanCreate(MaintenancePlanBase):
    device_id: UUID

class MaintenancePlanUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=255)
    description: Optional[str] = None
    priority: Optional[str] = Field(None, pattern="^(low|medium|high|urgent)$")
    frequency: Optional[PlanFrequency] = None
    interval_days: Optional[int] = Field(None, gt=0)
    estimated_duration: Optional[int] = Field(None, gt=0)
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    default_assignee: Optional[UUID] = None
    is_active: Optional[bool] = None
    auto_create_records: Optional[bool] = None
    advance_notice_days: Optional[int] = Field(None, ge=0, le=30)
    template_description: Optional[str] = None
    template_checklist: Optional[str] = None

class MaintenancePlanResponse(MaintenancePlanBase):
    id: UUID
    device_id: UUID
    last_execution: Optional[datetime] = None
    next_execution: Optional[datetime] = None
    is_active: bool
    auto_create_records: bool
    created_at: datetime
    updated_at: Optional[datetime] = None
    created_by: UUID
    
    class Config:
        from_attributes = True

# 维护计划执行结果
class MaintenancePlanExecution(BaseModel):
    execution_date: datetime
    next_execution: datetime
    created_record_id: Optional[UUID] = None

# 维护计划查询参数
class MaintenancePlanFilter(BaseModel):
    device_id: Optional[UUID] = None
    maintenance_type: Optional[MaintenanceType] = None
    frequency: Optional[PlanFrequency] = None
    is_active: Optional[bool] = None
    default_assignee: Optional[UUID] = None
    due_soon: Optional[bool] = None  # 即将到期的计划
    overdue: Optional[bool] = None   # 逾期的计划